package zk.weart.util.bean;


import zk.weart.util.bean.annotation.NotBind;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.security.SecureRandom;
import java.util.*;

/**
 * 功能描述  java bean 和hashMap之间的转换
 * @author ZKJ
 * @date 2021/6/16
 */
public class BeanMapUtils {

    private static final Random RANDOM = new SecureRandom();

    public BeanMapUtils() {
    }

    public static Map<String, String> toMap(Object obj) {
        HashMap map = new HashMap();

        try {
            if (obj == null) {
                return map;
            }

            Class cls = obj.getClass();
            Field[] fields = cls.getDeclaredFields();
            Field[] var4 = fields;
            int var5 = fields.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                Field f = var4[var6];
                f.setAccessible(true);
                String fileName = f.getName();
                NotBind notBind = (NotBind)f.getAnnotation(NotBind.class);
                if (notBind == null) {
                    Class fileType = f.getType();
                    Object value = f.get(obj);
                    if (value != null) {
                        map.put(fileName, value.toString());
                    }
                }
            }
        } catch (Exception var12) {
            var12.printStackTrace();
        }

        return map;
    }

    private static Map<String, String> supperToMap(Object obj) {
        HashMap map = new HashMap();

        try {
            if (obj == null) {
                return map;
            }

            Class cls = obj.getClass().getSuperclass();
            if (cls == null) {
                return map;
            }

            Field[] fields = cls.getDeclaredFields();
            Field[] var4 = fields;
            int var5 = fields.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                Field f = var4[var6];
                f.setAccessible(true);
                String fileName = f.getName();
                NotBind notBind = (NotBind)f.getAnnotation(NotBind.class);
                if (notBind == null) {
                    Class fileType = f.getType();
                    Object value = f.get(obj);
                    if (value != null) {
                        map.put(fileName, value.toString());
                    }
                }
            }
        } catch (Exception var12) {
            var12.printStackTrace();
        }

        return map;
    }

    public static Map<String, String> allToMap(Object obj) {
        new HashMap();
        Map<String, String> map = toMap(obj);
        map.putAll(supperToMap(obj));
        return map;
    }

    public static Object mapToBean(Map<String, String> map, Class cls) {
        try {
            if (map != null) {
                Constructor<?> cons = cls.getConstructor();
                Object obj = cons.newInstance();
                Field[] fields = cls.getDeclaredFields();
                Field[] var5 = fields;
                int var6 = fields.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    Field f = var5[var7];
                    String fileName = f.getName();
                    String value = (String)map.get(fileName);
                    if (value != null) {
                        Class typeClass = f.getType();
                        f.setAccessible(true);
                        if (typeClass.equals(Long.class)) {
                            f.set(obj, Long.parseLong(value));
                        } else if (typeClass.equals(Integer.class)) {
                            f.set(obj, Integer.parseInt(value));
                        } else if (typeClass.equals(Double.class)) {
                            f.set(obj, Double.parseDouble(value));
                        } else if (typeClass.equals(String.class)) {
                            f.set(obj, value);
                        }
                    }
                }

                return obj;
            }
        } catch (Exception var12) {
            var12.printStackTrace();
        }

        return null;
    }

    public static Object mapToBeanWithSupper(Map<String, String> map, Class cls) {
        try {
            if (map != null) {
                Constructor<?> cons = cls.getConstructor();
                Object obj = cons.newInstance();
                Field[] fields = cls.getDeclaredFields();
                List<Field> list = new ArrayList(Arrays.asList(fields));
                Class supperClass = cls.getSuperclass();
                if (supperClass != null) {
                    Field[] supperFields = supperClass.getDeclaredFields();
                    List<Field> supperList = new ArrayList(Arrays.asList(supperFields));
                    list.addAll(supperList);
                }

                Iterator var13 = list.iterator();

                while(var13.hasNext()) {
                    Field f = (Field)var13.next();
                    String fileName = f.getName();
                    String value = (String)map.get(fileName);
                    if (value != null) {
                        Class typeClass = f.getType();
                        f.setAccessible(true);
                        if (typeClass.equals(Long.class)) {
                            f.set(obj, Long.parseLong(value));
                        } else if (typeClass.equals(Integer.class)) {
                            f.set(obj, Integer.parseInt(value));
                        } else if (typeClass.equals(Double.class)) {
                            f.set(obj, Double.parseDouble(value));
                        } else if (typeClass.equals(String.class)) {
                            f.set(obj, value);
                        }
                    }
                }

                return obj;
            }
        } catch (Exception var12) {
            var12.printStackTrace();
        }

        return null;
    }

}
